package com.awesumgames.awesum.sprites;

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.Arrays;

import android.opengl.GLES20;
import android.opengl.Matrix;

import com.awesumgames.awesum.Game;
import com.awesumgames.awesum.Quaternion;
import com.awesumgames.awesum.RunnableTree;
import com.awesumgames.awesum.Vector3;
import com.awesumgames.awesum.AwesumGlobal.Awesum;

public class TileSet extends SpriteBase {
	public FloatBuffer vertices = null; /*Awesum.makeFloatBuffer(new float[]{
			0f,	0f,	0f,		0f, 1f,
			1f,	0f,	0f,		1f, 1f,
			1f,	1f,	0f,		1f, 0f,
			0f,	1f,	0f,		0f, 0f
	});*/
	public ShortBuffer indices = null; /*Awesum.makeByteBuffer(new byte[]{0, 1, 2,	2, 3, 0	});*/
	
	ArrayList<Float> data = new ArrayList<Float>();
	ArrayList<Short> dataIndices = new ArrayList<Short>();
	public int gridWidth = 0;
	public int gridHeight = 0;
	private float testx = 0f;
	
	public TileSet(int resFile, int width, int height) {
		super(resFile);
		//ArrayList<Float> v = new ArrayList<Float>();
		gridWidth = width;
		gridHeight = height;
	}
	
	public void addTile(int x, int y) {
		short zero = (short) ((short)data.size() / (short)5);
		SpriteCoords sc = SpriteBase.getCoords(x, y, gridWidth, gridHeight);
		Float[] newSprite = new Float[]{
			testx + 0f, 0f, 0f,		sc.x1, sc.y1,
			testx + .5f, 0f, 0f,		sc.x2, sc.y1,
			testx + .5f, .5f, 0f,		sc.x2, sc.y2,
			testx + 0f, .5f, 0f,		sc.x1, sc.y2
		};
		testx += .5f;
		//Awesum.debug("==============" + sc.pos.toString() + ", " + sc.size.toString());
		dataIndices.addAll(Arrays.asList(new Short[]{(short)(zero+0), (short)(zero+1), (short)(zero+2),	(short)(zero+2), (short)(zero+3), (short)(zero+0)}));
		data.addAll(Arrays.asList(newSprite));
	}
	
	public void draw() {
		if (!Game.relative)
			Matrix.setIdentityM(Game.modelMatrix, 0);
		Matrix.translateM(Game.modelMatrix, 0, pos.x, pos.y, pos.z);
		
		//Matrix.translateM(Game.modelMatrix, 0, translate.x, translate.y, translate.z);
		//Matrix.scaleM(Game.modelMatrix, 0, scale.x, scale.y, scale.z);
		if (!quat.isIdentity()) {
			//Vector3 axis = quat.axis();
			//Matrix.rotateM(Game.modelMatrix, 0, quat.angleDeg(), axis.x, 1f, axis.z);
		}
		
		vertices = Awesum.makeFloatBuffer(data.toArray(new Float[0]));
		indices = Awesum.makeShortBuffer(dataIndices.toArray(new Short[0]));
		
		//mvp
		GLES20.glUniformMatrix4fv(Game.Shaders.current.mvp, 1, false, Game.getMVP(), 0);
		
		//texture
		GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
		GLES20.glUniform1i(Game.Shaders.current.texture, 0);
        
		int stride = 5 * Game.FloatSize;
		
		vertices.position(0);
		GLES20.glVertexAttribPointer(Game.Shaders.current.pos, 3, GLES20.GL_FLOAT, false, stride, vertices);
		
		vertices.position(3);
		GLES20.glVertexAttribPointer(Game.Shaders.current.texCoord, 2, GLES20.GL_FLOAT, false, stride, vertices);
		
		GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.capacity(), GLES20.GL_UNSIGNED_SHORT, indices);
		
		super.step();
	}
}
